package com.andnnl.mongo;

import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;


public class MobileCodec implements Codec<MobileEntity> {
    public MobileCodec() {

    }

    /**
     * @see org.bson.codecs.Decoder#decode(org.bson.BsonReader, org.bson.codecs.DecoderContext)
     */
    @Override
    public MobileEntity decode(BsonReader reader, DecoderContext decoderContext) {
        MobileEntity user = new MobileEntity();
        reader.readStartDocument();
        user.setId(reader.readObjectId("_id").toString());
        user.setMobile(reader.readInt64("mobile"));
        user.setType(reader.readInt32("type"));

        reader.readEndDocument();
        return user;
    }

    /**
     * @see org.bson.codecs.Encoder#encode(org.bson.BsonWriter, Object, org.bson.codecs.EncoderContext)
     */
    @Override
    public void encode(BsonWriter writer, MobileEntity value, EncoderContext encoderContext) {
        writer.writeStartDocument();

        writer.writeInt64("mobile", value.getMobile());
        writer.writeInt32("type", value.getType());
        writer.writeEndDocument();
    }

    /**
     * @see org.bson.codecs.Encoder#getEncoderClass()
     */
    @Override
    public Class<MobileEntity> getEncoderClass() {
        return MobileEntity.class;
    }
}  